<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="submit"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-button type="primary" @click="varietyModalShow">选择产品</a-button>
      <a-form :form="form" >
        <a-form-item label="冷库" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input disabled='disabled' v-decorator="['storehouse', validatorRules.storehouse]"
                    placeholder="请选择冷库"/>
        </a-form-item>
<!--        <button>选择产品信息</button>-->
        <a-form-item label="采收产品" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input  disabled='disabled' v-decorator="[ 'variety', validatorRules.variety]"
                    placeholder="请选择产品"/>
          <a-input v-decorator="[ 'varietyCode', validatorRules.varietyCode]"
                   disabled='disabled'  hidden="true"/>
        </a-form-item>

        <a-form-item label="打包规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input  v-decorator="[ 'packingSpecifications', validatorRules.packingSpecifications]" disabled='disabled'
                    placeholder="请选择打包规格"/>
        </a-form-item>

        <a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'flowerPoleLength', validatorRules.flowerPoleLength]" disabled='disabled'
                    placeholder="请选择单位"/>
        </a-form-item>
        <a-form-item label="库损数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'loseNum', validatorRules.loseNum]" placeholder="请输入库损数量" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="库损类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select  v-decorator="[ 'loseType', validatorRules.loseType]" placeholder="请选择库损类型"  >
            <a-select-option v-for="(d,i) in loseTypeList"  :key="d.id" :value="d.title">{{ d.title }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
    <a-modal v-model:visible='varietyVisible' width='60%' title='产品选择' @ok="varietyAdd()">
      <varietyModal v-model:visible="varietyVisible" ref="varietyModal"/>
    </a-modal>
  </j-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { ajaxGetDictItems } from '@/api/api'
  import varietyModal from './varietyModal'


  export default {
    name: "FlowerLoseModal",
    components: {
      varietyModal
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {
          storehouse:'1',
          variety: '1',
          varietyCode: '1',
          packingSpecifications: '1',
          flowerPoleLength: '1',
          loseNum: '1',
          loseType: '1'
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        queryVarietyParams: {// 产品选择弹窗的查询
          variety: '',
          varietyCode: ''
        },
        confirmLoading: false,
        validatorRules: {
          storehouse: {
            rules: [{ required: true, message: '请输入冷库信息' }],
          },
          variety: {
            rules: [{ required: true, message: '请输入品类' }],
          },
          packingSpecifications: {
            rules: [{ required: true, message: '请输入打包规格' }],
          },
          flowerPoleLength: {
            rules: [{ required: true, message: '请输入单位' }],
          },
          loseNum: {
            rules: [{ required: true, message: '请输入损失花数' }],
          },
          loseType: {
            rules: [{ required: true, message: '请输入货损类型' }],
          },
        },
        url: {
          add: "/flower/flowerLose/add",
          edit: "/flower/flowerLose/edit",
        },
        classesList:[],
        psfList:[],
        lengthList:[],
        loseTypeList: [],
        varietyCode: '',
        classifyList: '',
        varietyVisible:false,
        firstModalLoading: false//第一个弹窗保存按钮是否处于加载中
      }
    },
    created () {
      this.initLoseTypeDict()//货损类型
    },
    methods: {
      varietyAdd(){
        let selectVariety = this.$refs.varietyModal.selectVariety;
        if(JSON.stringify(selectVariety) !== '{}'){
          this.form.setFieldsValue({
            storehouse: selectVariety.storehouse,
            variety: selectVariety.variety,
            varietyCode: selectVariety.varietyCode,
            packingSpecifications: selectVariety.packingSpecifications,
            flowerPoleLength: selectVariety.flowerPoleLength
          })
        }
        this.varietyVisible = false;
      },
      initLoseTypeDict(){
        const dictCode = 'lose_type'
        ajaxGetDictItems(dictCode, null).then((res) => {
          if (res.success) {
            this.loseTypeList = res.result;
          }
        })
      },
      varietyModalShow() {//打开第二个弹窗
        this.varietyVisible = !this.varietyVisible
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'variety','varietyCode','packingSpecifications','classes','flowerPoleLength','loseNum','loseType','classify'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      submit () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            this.firstModalLoading=true
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
              this.firstModalLoading=false
            }).finally(() => {
              this.firstModalLoading=false
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'variety','varietyCode','packingSpecifications','classes','flowerPoleLength','loseNum','loseType'))
      },


    }
  }
</script>